package com.hnkywl.sts.dao.cwgl.kmys;

import com.hnkywl.sts.entity.cwgl.kmys.Cykmdmys;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.awt.print.PageFormat;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-17
 * Time: 上午9:33
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class CykmdmysDao extends SimpleHibernateTemplate<Cykmdmys,Long>{
    public Page<Map> findCykmdmysList(Long gsId,String kmdm,int pageNo,int pageSize){
        Page<Map> page = new Page<Map>(pageNo, pageSize,0);
        String from = "from t_sys_cykmdmys cykmdmys left join t_sys_gsjg gsjg on (cykmdmys.Gsid = gsjg.id and gsjg.type = 'nbgs')";
        StringBuilder where = new StringBuilder();
        if(gsId!=null){
            if(where.length()!=0)
                where.append(" and ");
            where.append("gsjg.id = :gsId");
        }
        if(StringUtils.isNotEmpty(kmdm)){
            if(where.length()!=0)
                where.append(" and ");
            where.append("cykmdmys.kmdm like '"+kmdm+"%'");
        }
        String orderBy = " order by cykmdmys.codeId asc";
        String countSql = "select count(1) " + from+ (StringUtils.isNotEmpty(where.toString())?" where " : "") + where;
        String sql = "select cykmdmys.id as \"cykmdmys.id\", cykmdmys.codeId as \"codeId\", gsjg.jc as \"gsjgJc\"," +
                " cykmdmys.jqcwkmmc as \"cykmdmys.jqcwkmmc\", cykmdmys.kmdm as \"cykmdmys.kmdm\" , " +
                " cykmdmys.sl as \"cykmdmys.sl\"  "+
                from  + (StringUtils.isNotEmpty(where.toString())?" where " : "") + where + orderBy;
        SQLQuery countSqlQuery = getSession().createSQLQuery(countSql);
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);
        if(gsId!=null){
            countSqlQuery.setParameter("gsId",gsId);
            sqlQuery.setParameter("gsId",gsId);
        }
        if(page.isFirstSetted())
            sqlQuery.setFirstResult(page.getFirst());
        if(page.isPageSizeSetted())
            sqlQuery.setMaxResults(page.getPageSize());
        sqlQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        page.setTotalCount(((BigDecimal)countSqlQuery.uniqueResult()).intValue());
        page.setResult(sqlQuery.list());
        return page;
    }

    public void deleteByGsId(Long gsId){
        String hqlString = "delete from Cykmdmys where gsId = :gsId";
        Query query = getSession().createQuery(hqlString);
        query.setParameter("gsId",gsId);
        query.executeUpdate();
    }


    public Cykmdmys findKmdmByMc(Long compid,String code){
        String   hql = " from  Cykmdmys where gsId = ? and jqcwkmmc = ?";
        return  (Cykmdmys)find(hql,compid,code).get(0);
    }


    public Cykmdmys getCommentSubject(Long compid,String code){
        String   hql = " from  Cykmdmys where gsId = ? and codeid = ?";
        return  (Cykmdmys)find(hql,compid,code).get(0);
    }

    public List<Cykmdmys> findSlCykmdmysList(Long compid){
        String   hql = " from  Cykmdmys where gsId = ? and sl = 1 ";
        return  ( List<Cykmdmys>)find(hql,compid);
    }
}
